package com.example.aicodehelper.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfiguration implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //:允许所有路径的CORS请求。
        registry.addMapping("/**")
                // 允许来自http://localhost:5173的请求
//                .allowedOrigins("http://localhost:5173")
                .allowedOriginPatterns("*")     // 使用匹配模式而不是allowedOrigins
                // 允许所有请求方法
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD")
                // 允许的请求头,
                .allowedHeaders("*")
                .exposedHeaders("*")
                //:允许发送身份凭证(如cookies)
                .allowCredentials(true).maxAge(3600);

//        特别为SockJS添加端点配置
        registry.addMapping("/snow-stomp/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*").allowedHeaders("*")
                .allowCredentials(true).maxAge(3600);

//        添加对SockJS/info端点的配置
        registry.addMapping("/snow-stomp/info/**").allowedOriginPatterns("*")
                .allowedMethods("*").allowedHeaders("*").allowCredentials(true).maxAge(3600);
    }


}
